package com.qf.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.baseservice.impl.BaseServiceImpl;
import com.qf.entity.Goods;
import com.qf.entity.User;
import com.qf.mapper.IGoodsMapper;
import com.qf.service.IGoodsService;
import com.qf.service.ISearchService;
import com.qf.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author 刘亚康
 * @Date 2020/3/27
 * @Version 1.0'
 * @Description
 */
@Slf4j
@Service
public class GoodsServiceImpl extends BaseServiceImpl<Goods> implements IGoodsService {
    @Reference
    private ISearchService searchService;

    @Autowired
    private IGoodsMapper goodsMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    protected BaseMapper<Goods> getBaseMapper() {
        return goodsMapper;
    }

    @Override
    public int add(Goods goods) {
        log.info("添加商品："+goods);
        int add = super.add(goods);
//        searchService.addGoodsToSolr(goods);
//        HttpUtils.sendUrlUtils("http://localhost:8084/addGoodsPage?goodsId="+goods.getGoodsId());
        rabbitTemplate.convertAndSend("goodsExchange","",goods);
        return add;
    }

    /**
     * 分页查询商品
     *
     * @param page
     * @return
     */
    @Override
    public Page<Goods> getGoodsByPage(Page<Goods> page) {
        log.info("开始分页查询");
        long current=page.getCurrent();
        long size=page.getSize();
        long start=(current-1)*size;
        goodsMapper.selectPage(page,null);
        List<Goods> goodsList=goodsMapper.getGoodsByPage(start,size);
        page.setRecords(goodsList);
        return page;
    }
}
